<script type="text/javascript">
(function(){
	var score = 100;
	var errNum = 0;
	var warnNum = 0;

	$("#Start").click(function(){
		$(this).hide();
		start();
	});

	function checkUpgrade(){
		var para = {
			system:{upgrade_info:{action:"start"}}
		};

		apiGet(para);
	}

	function start(){
		score = 100;
		errNum = 0;
		warnNum = 0;

		$("p.init-state").hide();
		$("p.score").show();
		$("ul.state-con .init-value").show();
		$("ul.state-con .state-icon").hide();
		$("ul.sub-state").hide();
		$("ul.state-con li.wan-error-info").hide();
		$("div.running-circle").addClass("run-animation");
		$("p.tips").text("");

		setScore(score, 0);
		checkUpgrade();	// 通知后台进行新版本检测
		getWanLinkState();
	}

	function setScore(score, type){
		$("span.score-num").text(score);

		if (score > 70){
			$(".running-status").removeClass("state-middle state-warn").addClass("state-ok");
		}else if (score > 30){
			$(".running-status").removeClass("state-ok state-warn").addClass("state-middle");
		}else{
			$(".running-status").removeClass("state-ok state-middle").addClass("state-warn");
		}

		switch(type){
		case 1:
			errNum++;
			break;
		case 2:
			warnNum++;
			break;
		case 0:
		default:
			break;
		}

		if (warnNum > 0 || errNum > 0){
			var tips = "" + errNum + label.problems + warnNum + label.warmTips;
			$("p.tips").text(tips);
		}else{
			$("p.tips").text(label.noProblem);
		}
	}

	function getWanLinkState(){
		$("#WanState .init-value").hide();

		var para = {
			network:{wan_status:null}
		};

		apiGet(para, function(data){
			var wanStatus = data[K_MODULE]["network"]["wan_status"];

			var code = wanStatus["status_code"];
			if ("1" == code){	// WAN口未连接
				$("#WanState li.state-con").removeClass("state-good").addClass("state-bad");
				$("#WanState i.icon_remind").show();
				$("#NetState").hide();

				$("#WanState").show();
				$("#WanState .wan-error-info").show();
				score -= 30;
				setScore(score, 1);
				$.setTimeout(getPowerState, 1000);
			}else{
				$("#WanState li.state-con").removeClass("state-bad").addClass("state-good");
				$("#WanState i.icon_select_on").show();

				setScore(score, 0);
				$.setTimeout(function(){
					initInternetState(wanStatus);
				}, 1000);
			}
		});
	}

	function initInternetState(wanStatus){
		$("#NetState .init-value").hide();

		var status = wanStatus["status_code"];
		if ("0" == status){
			$("#NetState li").removeClass("state-bad").addClass("state-good");
			$("#NetState i.icon_select_on").show();
		}else{
			/* status_code:
			  0: okay,
			  1: wan port not link,
			  2: no wan address,
			  3: gateway no response,
			  4: no DNS address,
			  5: DNS no response,
			  6: custom DNS error,
			  7: DHCP server no response,
			  8: getting DHCP IP,
			  9: PPPoE diaging,
			  10: PPPoE server no response,
			  646~649: PPPOE error,
			  678: PPPOE server error,
			  691: PPPOE account invalid,
			  709: PPPOE password invalid
			*/
			var code = parseInt(wanStatus["status_code"], 10);
			var protocol = wanStatus["protocol"];

			switch(protocol){
			case "static":
				errorInfo = label.staicIpErr;
				break;
			case "dhcp":
				switch(code){
				case 3:
					errorInfo = label.wanGatewayErr;
					break;
				case 4:
					errorInfo = label.wanDnsErr;
					break;
				case 5:
					errorInfo = label.wanNetErr;
					break;
				case 6:
					errorInfo = label.wanDnsCfgErr;
					break;
				case 7:
				case 8:
				default:
					errorInfo = label.wanDhcpErr;
					break;
				}

				break;
			case "pppoe":
				switch(code){
				case 709:
					errorInfo = label.pppoePwdErr;
					break;
				case 691:
					errorInfo = label.pppoeAccountErr;
					break;
				case 678:
					errorInfo = label.pppoeSrvErr;
					break;
				case 646:
				case 647:
				case 648:
				case 649:
					errorInfo = label.pppoeCodeL + code + label.pppoeCodeR;
					break;
				case 3:
					errorInfo = label.wanGatewayErr;
					break;
				case 4:
					errorInfo = label.wanDnsErr;
					break;
				case 5:
					errorInfo = label.wanNetErr;
					break;
				case 6:
					errorInfo = label.wanDnsCfgErr;
					break;
				case 9:
					errorInfo = label.pppoeDialing;
					break;
				case 10:
					errorInfo = label.pppoeNoResponse;
					break;
				}
				break;
			default:
				console.log("Warning:protocol=" + protocol);
			}
			$("#NetState li.state-con").removeClass("state-good").addClass("state-bad");
			$("#NetState .wan-error-info").show();
			$("#WanErrDetail").html(errorInfo);
			$("#NetState i.icon_remind").show();
			score -= 15;
			setScore(score, 2);
		}

		$("#NetState").show();
		$.setTimeout(getPowerState, 2000);
	}

	function saveApi(module, func, para, callback){
		var data = {};
		data[module] = {};
		data[module][func] = para;

		apiSet(data, callback);
	}

	function getPowerState(){
		$("#WifiState .init-value").hide();
		var para = {
			signal_set:{config:null}
		};
		apiGet(para, function(data){
			var signal = data[K_MODULE]["signal_set"];
			var config = signal.config;

			if(config.power == 0)//2.4G信号增强未开启
			{
				score -= 10;
				setScore(score, 2);
				$("#WifiSignal").removeClass("state-good").addClass("state-bad");

				initSwitch("SwitchWifiSignal", 0, function(state, init){
					if (!init){
						saveApi("signal_set", "config", {power:state + ""});
					}
				});
			}else{
				$("#WifiSignal").removeClass("state-bad").addClass("state-good");
			}

			getWifiState(signal);
		});
	}

	function getWifiState(signal){
		var para = {
			wireless:{wifi_2g_config:null, wifi_5g_config:null}
		};

		apiGet(para, function(data){
			var wireless = data[K_MODULE]["wireless"];
			var wifi2G = wireless.wifi_2g_config;
			var wifi5G = wireless.wifi_5g_config;

			if(wifi2G.enable == 0)//2.4G无线未开启
			{
				score -= 10;
				setScore(score, 1);
				$("#Wifi2G").removeClass("state-good").addClass("state-bad");

				initSwitch("SwitchWifi2G", 0, function(state, init){
					if (!init){
						var para = {};
						para.wireless = {};
						para.wireless.wifi_2g_config = {enable:state + ""};
						para.wireless.wifi_5g_config = {enable:wifi5G.enable + ""};
						showLoading(label.saveWifi);
						apiSet(para, function(ret){
							var time = ret[K_MODULE].wireless.wifi_5g_config.wait_time;
							$.setTimeout(function(){
								closeLoading();
							}, time * 1000);
						});
					}
				});
			}else{
				$("#Wifi2G").removeClass("state-bad").addClass("state-good");
			}

			if(wifi5G.enable == 0)//5G无线未开启
			{
				score -= 10;
				setScore(score, 1);
				$("#Wifi5G").removeClass("state-good").addClass("state-bad");

				initSwitch("SwitchWifi5G", 0, function(state, init){
					if (!init){
						showLoading(label.saveWifi);
						saveApi("wireless", "wifi_5g_config", {enable:state + ""}, function(ret){
							var time = ret[K_MODULE].wireless.wifi_5g_config.wait_time;
							$.setTimeout(function(){
								closeLoading();
							}, time * 1000);
						});
					}
				});
			}else{
				$("#Wifi5G").removeClass("state-bad").addClass("state-good");
			}

			if(wifi2G.enable == 1 && wifi5G.enable == 1 && signal.config.power == 1)
			{
				$("#WifiState").removeClass("state-bad").addClass("state-good");
				$("#WifiState i.icon_select_on").show();
			}else{
				$("#WifiState").removeClass("state-good").addClass("state-bad");
				$("#WifiState i.icon_remind").show();
			}

			$("#WifiStateCon").show();

			$.setTimeout(function(){
				initWifiPwdState(wireless);
			}, 2000);
		});
	}

	function getSafety(pwd){
		var safety = 0;

		if (/[A-Z]/g.test(pwd)){
			safety += 1;
		}

		if (/[a-z]/g.test(pwd)){
			safety += 1;
		}

		if (/[0-9]/g.test(pwd)){
			safety += 1;
		}

		if (/[^0-9a-zA-Z]/g.test(pwd)){
			safety += 3;
		}

		return safety;
	}

	function initWifiPwdState(wireless){
		$("#WifiPwdState .init-value").hide();

		var pwd2G = wireless.wifi_2g_config.password;
		var safety2G = getSafety(pwd2G);
		if(safety2G <= 1)
		{
			score -= 9;
			setScore(score, 2);
			$("#WifiPwd2G").removeClass("state-good").addClass("state-bad");
			$("#WifiPwd2G span.state-desc").text(label.wifi2GPwdWeak).removeClass("state-good").addClass("state-bad");
			$("#WifiPwd2G btn-modify").show();
		}
		else if(safety2G <= 2)
		{
			score -= 5;
			setScore(score, 2);
			$("#WifiPwd2G").removeClass("state-good").addClass("state-bad");
			$("#WifiPwd2G span.state-desc").text(label.wifi2GPwdMid).removeClass("state-good").addClass("state-bad");
			$("#WifiPwd2G btn-modify").show();
		}else{
			score -= 0;
			setScore(score, 0);
			$("#WifiPwd2G").removeClass("state-bad").addClass("state-good");
			$("#WifiPwd2G span.state-desc").text(label.wifi2GPwdStrong).removeClass("state-bad").addClass("state-good");
			$("#WifiPwd2G a.btn-modify").hide();
		}

		var pwd5G = wireless.wifi_5g_config.password;
		var safety5G = getSafety(pwd5G);
		if(safety5G <= 1){
			score -= 9;
			setScore(score, 2);
			$("#WifiPwd5G").removeClass("state-good").addClass("state-bad");
			$("#WifiPwd5G span.state-desc").text(label.wifi5GPwdWeak).removeClass("state-good").addClass("state-bad");
			$("#WifiPwd5G a.btn-modify").show();
		}else if(safety5G <= 2){
			score -= 5;
			setScore(score, 2);
			$("#WifiPwd5G").removeClass("state-good").addClass("state-bad");
			$("#WifiPwd5G span.state-desc").text(label.wifi5GPwdMid).removeClass("state-good").addClass("state-bad");
			$("#WifiPwd5G a.btn-modify").show();
		}else{
			score -= 0;
			setScore(score, 0);
			$("#WifiPwd5G").removeClass("state-bad").addClass("state-good");
			$("#WifiPwd5G span.state-desc").text(label.wifi5GPwdStrong).removeClass("state-bad").addClass("state-good");
			$("#WifiPwd5G a.btn-modify").hide();
		}

		if(safety2G >= 3 && safety5G >= 3){
			$("#WifiPwdState").removeClass("state-bad").addClass("state-good");
			$("#WifiPwdState i.icon_select_on").show();
		}else{
			$("#WifiPwdState").removeClass("state-good").addClass("state-bad");
			$("#WifiPwdState i.icon_remind").show();
		}

		$("#WifiPwdStateCon").show();
		$.setTimeout(getRouterPwdState, 2000);
	}

	function getRouterPwdState(){
		$("#RouterPwdState .init-value").hide();

		var para = {
			security:{status:null}
		};

		apiGet(para, function(data){
			var security = data[K_MODULE]["security"];
			var safety = security.status.safety;

			if(safety == 1){
				score -= 9;
				setScore(score, 2);
				$("#RouterPwdState").removeClass("state-good").addClass("state-bad");
				$("#RouterPwdState i.icon_remind").show();
				$("#RouterPwd").removeClass("state-good").addClass("state-bad");
				$("#RouterPwd span.state-desc").text(label.routerPwdWeak).removeClass("state-good").addClass("state-bad");
				$("#RouterPwd a.btn-modify").show();
			}else if(safety == 2){
				score -= 5;
				setScore(score, 2);
				$("#RouterPwdState").removeClass("state-good").addClass("state-bad");
				$("#RouterPwdState i.icon_remind").show();
				$("#RouterPwd").removeClass("state-good").addClass("state-bad");
				$("#RouterPwd span.state-desc").text(label.routerPwdMid).removeClass("state-good").addClass("state-bad");
				$("#RouterPwd a.btn-modify").show();
			}else{
				score -= 0;
				setScore(score, 0);
				$("#RouterPwdState").removeClass("state-bad").addClass("state-good");
				$("#RouterPwdState i.icon_select_on").show();
				$("#RouterPwd").removeClass("state-bad").addClass("state-good");
				$("#RouterPwd span.state-desc").text(label.routerPwdStrong).removeClass("state-bad").addClass("state-good");
				$("#RouterPwd a.btn-modify").hide();
			}

			$("#RouterPwdStateCon").show();
			$.setTimeout(getUpgradeState, 2000);
		});
	}

	function getUpgradeState(){
		$("#UpgradeState .init-value").hide();

		var para = {
			system:{upgrade_info:{action:"get"}}
		};

		apiGet(para, function(data){
			var info = data[K_MODULE]["system"]["upgrade_info"];

			var status = info.running_status;
			if ("2" == status){	// 升级检查完成
				if ("1" == info.status_code){	// 有新版本
					$("#UpgradeState").removeClass("state-good").addClass("state-bad");
					$("#UpgradeState i.icon_remind").show();
					$("#UpgradeDetail").removeClass("state-good").addClass("state-bad");
					$("#UpgradeDetail span.state-desc").text(label.hasNewSw).removeClass("state-good").addClass("state-bad");
					$("#UpgradeDetail a.btn-modify").show();
					score -= 10;
					setScore(score, 2);
				}else{
					$("#UpgradeState").removeClass("state-bad").addClass("state-good");
					$("#UpgradeState i.icon_select_on").show();
					$("#UpgradeDetail span.state-desc").text(label.swIsNewest).removeClass("state-bad").addClass("state-good");
					$("#UpgradeDetail a.btn-modify").hide();
				}

				$("#UpgradeStateCon").show();
				$("div.running-circle").removeClass("run-animation");
				$("#Start").val(btn.reDiagnose).show();
			}else{
				$.setTimeout(getUpgradeState, 2000);
			}
		});
	}
})();
</script>
<style type="text/css">
	div.diagnose-con{
		position:relative;
		margin:20px 50px 10px;
	}
	span.switch{
		margin-right:0;
		display:inline-block !important;
	}
	ul.state-con{
		margin-left:35%;
		border-bottom:1px solid #DDD;
	}
	li.state-con{
		font-size:0;
		height:45px;
		line-height:45px;
		vertical-align:middle;
	}
	i.state-icon{
		float:right;
		font-size:30px;
		line-height:45px;
	}
	i.icon_select_on{
		display:none;
		color:#5DBD38;
	}
	i.icon_remind{
		display:none;
		color:#FF0101;
	}
	span.state-desc{
		vertical-align:top;
		color:#403F44;
		font-size:14px;
	}
	li.sub-state{
		text-align:right;
	}
	li.sub-state span.state-desc{
		float:left;
		margin-left:150px;
	}
	li.border-bottom{
		border-bottom:1px solid #DDD;
	}
	li.sub-state span.state-good{
		color:#9C9C9C;
	}
	li.sub-state span.state-bad{
		color:#FF0101;
	}
	li.state-good span.state-good{
		display:inline;
	}
	li.state-good span.switch{
		visibility:hidden;
	}
	li.state-good span.state-bad{
		display:none;
	}
	li.state-bad span.state-good{
		display:none;
	}
	li.state-bad span.state-bad{
		display:inline;
	}
	span.init-value{
		float:right;
		color:#9C9C9C;
		font-size:14px;
	}
	a.btn-modify{
		color:#FF0101;
		float:right;
		font-size:14px;
		text-decoration:none;
	}
	li.wan-error-info{
		position:relative;
		margin-bottom:5px;
	}
	li.wan-error-info a.btn-modify{
		position:absolute;
		top:0;
		right:0;
	}
	p.error-detail{
		color:#FF0101;
		font-size:14px;
		margin-right:50px;
	}
	div.running-con{
		position:absolute;
		width:35%;
		text-align:center;
	}
	.running-status{
		position:relative;
		margin:50px auto 30px;
		color:#03CB9C;
		text-align:center;
	}
	.running-circle{
		display:inline-block;
		color:#03CB9C;
		width:170px;
		height:170px;
		border-radius:50%;
		background-color:#03CB9C;
		background-image:url(/luci-static/images/circle.png?_=20170809165512);
	}
	.run-animation{
		-webkit-animation:circle 2s infinite linear;
		-moz-animation:circle 2s infinite linear;
		-ms-animation:circle 2s infinite linear;
		animation:circle 2s infinite linear;
	}
	@-webkit-keyframes circle{
		0%{-webkit-transform:rotate(0deg);}
		100%{-webkit-transform:rotate(360deg);}
	}
	@-moz-keyframes circle{
		0%{-moz-transform:rotate(0deg);}
		100%{-moz-transform:rotate(360deg);}
	}
	@-ms-keyframes circle{
		0%{-ms-transform:rotate(0deg);}
		100%{-ms-transform:rotate(360deg);}
	}
	@keyframes circle{
		0%{transform:rotate(0deg);}
		100%{transform:rotate(360deg);}
	}
	.state-ok{
		color:#03CB9C !important;
	}
	.state-ok div.running-circle{
		background-color:#03CB9C;
	}
	.state-middle{
		color:#F68132 !important;
	}
	.state-middle div.running-circle{
		background-color:#F68132;
	}
	.state-warn{
		color:#EE5859 !important;
	}
	.state-warn div.running-circle{
		background-color:#EE5859;
	}
	p.init-state{
		position:absolute;
		top:75px;
		width:100%;
		font-size:18px;
	}
	p.score{
		position:absolute;
		top:55px;
		font-size:18px;
		width:100%;
	}
	span.score-num{
		font-family:icoset,sans-serif;
		font-size:50px;
	}
	span.score{
		font-size:14px;
	}
	div.running-con p.tips{
		font-size:14px;
		color:#403F44;
	}
	input.start{
		color:#FFF;
		bottom:0.15rem;
		right:0.45rem;
		position:absolute;
		background-color:transparent;
		width:2.75rem;
		height:0.75rem;
		border-radius:0.5rem;
		font-size:0.45rem;
		border:1px solid;
	}
	/*1080p适配*/
	@media screen and (min-device-width:1920px){
		span.state-desc,
		span.init-value,
		p.error-detail,
		div.running-con p.tips,
		a.btn-modify{
			font-size: 18px;
		}
	}
</style>
<div class="help hidden">
	<ul class="help-content">
		<li class="help-head">
			<img class="app-icon" src="/luci-static/images/app-icon/diagnose.png?_=20170809165512" />
			<span class="help-title">{%label.diagnose%}</span>
		</li>
		<li class="summary">{%helpStr.diagnose%}</li>
	</ul>
</div>
<div class="head-con">
	<img class="app-icon" src="/luci-static/images/app-icon/diagnose.png?_=20170809165512" />
	<a href="javascript:history.go(-1);" class="head-btn-l">
		<i class="head-btn icon icon_back"></i>
		<span class="head-title">{%label.diagnose%}</span>
	</a>
</div>
<div class="set-con diagnose-con">
	<div class="running-con">
		<div class="running-status">
			<div class="running-circle"></div>
			<p class="init-state">{%label.unDiagnose%}</p>
			<p class="score hidden"><span class="score-num">100</span><span class="score">{%label.score%}</span></p>
		</div>
		<p class="tips"></p>
	</div>
	<div class="diagnose-info">
		<ul class="state-con" id="WanState">
			<li class="state-con">
				<i class="state-icon icon_select_on"></i><i class="state-icon icon_remind"></i>
				<span class="state-desc">{%label.wanState%}</span><span class="init-value">{%label.waittingDiagnose%}</span>
			</li>
			<li class="wan-error-info hidden">
				<p class="error-detail">{%label.wanNotLinkTip%}</p>
			</li>
		</ul>
		<ul class="state-con" id="NetState">
			<li class="state-con">
				<i class="state-icon icon_select_on"></i><i class="state-icon icon_remind"></i>
				<span class="state-desc">{%label.internetState%}</span><span class="init-value">{%label.waittingDiagnose%}</span>
			</li>
			<li class="wan-error-info hidden">
				<p id="WanErrDetail" class="error-detail"></p>
				<a href="#/pc/networksetApp" class="btn-modify">{%btn.modify%}</a>
			</li>
		</ul>
		<ul class="state-con">
			<li class="state-con" id="WifiState">
				<i class="state-icon icon_select_on"></i><i class="state-icon icon_remind"></i>
				<span class="state-desc">{%label.wifiState%}</span><span class="init-value">{%label.waittingDiagnose%}</span>
			</li>
		</ul>
		<ul class="state-con sub-state hidden" id="WifiStateCon">
			<li class="state-con sub-state border-bottom" id="Wifi2G">
				<i class="state-icon invisible"></i>
				<span class="state-desc state-good hidden">{%label.wifi2GOpen%}</span>
				<span class="state-desc state-bad">{%label.wifi2GClose%}</span>
				<span id="SwitchWifi2G" class="switch state-bad"><i class="switch-circle"></i></span>
			</li>
			<li class="state-con sub-state border-bottom" id="Wifi5G">
				<i class="state-icon invisible"></i>
				<span class="state-desc state-good hidden">{%label.wifi5GOpen%}</span>
				<span class="state-desc state-bad">{%label.wifi5GClose%}</span>
				<span id="SwitchWifi5G" class="switch state-bad"><i class="switch-circle"></i></span>
			</li>
			<li class="state-con sub-state" id="WifiSignal">
				<i class="state-icon invisible"></i>
				<span class="state-desc state-good hidden">{%label.wifiSignalOpen%}</span>
				<span class="state-desc state-bad">{%label.wifiSignalClose%}</span>
				<span id="SwitchWifiSignal" class="switch state-bad"><i class="switch-circle"></i></span>
			</li>
		</ul>
		<ul class="state-con">
			<li class="state-con" id="WifiPwdState">
				<i class="state-icon icon_select_on"></i><i class="state-icon icon_remind"></i>
				<span class="state-desc">{%label.wifiPwdState%}</span><span class="init-value">{%label.waittingDiagnose%}</span>
			</li>
		</ul>
		<ul class="state-con sub-state hidden" id="WifiPwdStateCon">
			<li class="state-con sub-state border-bottom" id="WifiPwd2G">
				<i class="state-icon invisible"></i>
				<span class="state-desc state-bad">{%label.wifi2GPwdWeak%}</span>
				<a href="#/pc/wifiSet" class="btn-modify">{%btn.modify%}</a>
			</li>
			<li class="state-con sub-state" id="WifiPwd5G">
				<i class="state-icon invisible"></i>
				<span class="state-desc state-bad">{%label.wifi5GPwdWeak%}</span>
				<a href="#/pc/wifiSet" class="btn-modify">{%btn.modify%}</a>
			</li>
		</ul>
		<ul class="state-con">
			<li class="state-con" id="RouterPwdState">
				<i class="state-icon icon_select_on"></i><i class="state-icon icon_remind"></i>
				<span class="state-desc">{%label.routerPwdState%}</span><span class="init-value">{%label.waittingDiagnose%}</span>
			</li>
		</ul>
		<ul class="state-con sub-state hidden" id="RouterPwdStateCon">
			<li class="state-con sub-state" id="RouterPwd">
				<i class="state-icon invisible"></i>
				<span class="state-desc state-bad">{%label.routerPwdWeak%}</span>
				<a href="javascript:showModifyPwd();" class="btn-modify">{%btn.modify%}</a>
			</li>
		</ul>
		<ul class="state-con">
			<li class="state-con state-good" id="UpgradeState">
				<i class="state-icon icon_select_on"></i><i class="state-icon icon_remind"></i>
				<span class="state-desc">{%label.swUpgradeState%}</span><span class="init-value">{%label.waittingDiagnose%}</span>
			</li>
		</ul>
		<ul class="state-con sub-state hidden" id="UpgradeStateCon">
			<li class="state-con sub-state" id="UpgradeDetail">
				<i class="state-icon invisible"></i>
				<span class="state-desc state-bad">{%label.hasNewSw%}</span>
				<a href="#/pc/autoUpgradeApp" class="btn-modify">{%btn.upgrade%}</a>
			</li>
		</ul>
		<div class="btn-con">
			<input id="Start" class="btn" type="button" value="{%btn.clickDiagnose%}" />
		</div>
	</div>
</div>
